{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d558f4d2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from mindquantum.core.gates import X, H, Z, RX, RY, RZ, CNOT\n",
    "from mindquantum.core.gates import Measure\n",
    "from mindquantum.core.circuit import Circuit\n",
    "from IPython.display import display_svg"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1697a969",
   "metadata": {},
   "source": [
    "## 模块化设计量子线路"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d0db7ddf",
   "metadata": {},
   "outputs": [],
   "source": [
    "def create_EPR_state(p: int, q: int) -> Circuit:\n",
    "    \"\"\"制备EPR Pair\n",
    "    Args:\n",
    "        p: 贝尔态作用第1个位置\n",
    "        q: 贝尔态作用第2个位置\n",
    "    Return:\n",
    "        制备贝尔态线路\n",
    "    \"\"\"\n",
    "    return Circuit([\n",
    "        H(p),\n",
    "        X(q, p)\n",
    "    ])\n",
    "\n",
    "def create_random_state(p: int) -> Circuit:\n",
    "    \"\"\"在1个比特上制备随机态，通过随机旋转初态制备\n",
    "    Args:\n",
    "        p: 随机值作用比特\n",
    "    Return:\n",
    "        处于随机的单量子态\n",
    "    \"\"\"\n",
    "    # 使用RX, RY, RZ随机旋转角度作用在 |0> 上实现随机状态制备\n",
    "    t1, t2, t3 = 2 * np.pi * np.random.random(size=3)\n",
    "    return Circuit([\n",
    "        RX(t1).on(p), # 不使用函数 on() 直接使用 RX(t1)(p) 也可\n",
    "        RY(t2).on(p),\n",
    "        RZ(t3).on(p)\n",
    "    ])\n",
    "\n",
    "def create_basic_module(p: int, q: int) -> Circuit:\n",
    "    \"\"\"CNOT,H,Measure 的组合在本位量子通信经常用到，综合为一个接口\n",
    "    Args:\n",
    "        p: 第1个量子位置\n",
    "        q: 第2个量子位置\n",
    "    Return:\n",
    "        量子线路模块\n",
    "    \"\"\"\n",
    "    return Circuit([\n",
    "        CNOT(q, p),\n",
    "        H(p),\n",
    "        Measure(f'q{p}').on(p),\n",
    "        Measure(f'q{q}').on(q)\n",
    "    ])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "07b6c529",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_measure_result(ket_str: str, idx: int or list) -> int or list:\n",
    "    \"\"\"获取指定状态的测量结果，通过解析 get_qs(ket=True) 的返回值实现\n",
    "    Args:\n",
    "        ket_str: get_qs(ket=True)获取的量子态\n",
    "        idx: 需要获取的量子位\n",
    "    Return:\n",
    "        指定量子位的测量结果，\n",
    "    \"\"\"\n",
    "    ket_str2 = ket_str.split('\\n')[0]\n",
    "    if isinstance(idx, int):\n",
    "        new_idx = -idx - 2\n",
    "        return ket_str2[new_idx]\n",
    "    if isinstance(idx, list):\n",
    "        return [ket_str2[-i-2] for i in idx]\n",
    "    print(\"Error: idx should be int or list!\")\n",
    "    return -1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02ae2213",
   "metadata": {},
   "source": [
    "## 论文线路复现"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c265447f",
   "metadata": {},
   "source": [
    "### 案例1\n",
    "\n",
    "- Fig.3 量子瞬时传输线路\n",
    "\n",
    "<img src=\"images/fig3.jpg\" width=400>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0f839567",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fig.3\n",
      "complete circuit:\n",
      "\n"
     ]
    },
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"576.8\" height=\"200.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"576.8\" height=\"200.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q1: </text><text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q2: </text><line x1=\"48.8\" x2=\"556.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"556.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"556.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"72.8\" y=\"20.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"112.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RX </text><text x=\"112.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >1.0494 </text><rect x=\"172.8\" y=\"20.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"212.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RY </text><text x=\"212.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >3.3889 </text><rect x=\"272.8\" y=\"20.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"312.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RZ </text><text x=\"312.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >3.7352 </text><rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><circle cx=\"152.8\" cy=\"100.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"132.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"138.8\" x2=\"166.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"146.0\" y2=\"174.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><circle cx=\"392.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"392.8\" x2=\"392.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"372.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"378.8\" x2=\"406.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"392.8\" x2=\"392.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><rect x=\"432.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"452.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><rect x=\"492.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"512.8\" cy=\"50.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 499.99999999999994 50.4 A 12.8 12.8 0 0 1 525.5999999999999 50.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 516.9018483174859 33.93539030917347 L 525.2156921938165 29.135390309173467 L 525.2156921938165 38.73539030917347 L 521.8901546432843 36.815390309173466 L 514.0470765814496 50.4 L 512.3843078061834 49.44 L 520.2273858680182 35.85539030917347 Z\" fill=\"#ffffff\" /><rect x=\"432.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"452.8\" cy=\"110.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 440.0 110.4 A 12.8 12.8 0 0 1 465.6 110.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 456.9018483174859 93.93539030917347 L 465.21569219381655 89.13539030917347 L 465.21569219381655 98.73539030917347 L 461.8901546432843 96.81539030917347 L 454.04707658144963 110.4 L 452.3843078061835 109.44 L 460.2273858680182 95.85539030917347 Z\" fill=\"#ffffff\" /><rect x=\"192.8\" y=\"140.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"212.8\" y=\"160.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >X </text></svg>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Measure result:\n",
      "q0=0, q1=1\n",
      "\n",
      "Send state ¦qn,...,q0⟩:\n",
      "(0.5066002050174433-0.043308464530512736j)¦0⟩\n",
      "(-0.3102524129481916+0.8032583951855832j)¦1⟩\n",
      "\n",
      "Recv state ¦qn,...,q0⟩:\n",
      "(0.5066002050174432-0.043308464530512736j)¦010⟩\n",
      "(-0.3102524129481916+0.8032583951855832j)¦110⟩\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def simulate_fig3():\n",
    "    \"\"\"复现论文 Fig3 的线路，实现 EPR 传输\n",
    "    \"\"\"\n",
    "    cir_send = create_random_state(0)      # 初始需要传输的态，对应论文中 |y>\n",
    "    cir_epr12 = create_EPR_state(1, 2)    # 制备 EPR\n",
    "    cir_mod01 = create_basic_module(0, 1)  # 线路\n",
    "\n",
    "    # 使用同一个随机数种子，避免各次测量结果不同\n",
    "    seed = np.random.randint(0, 0xff)            # 随机数种子\n",
    "    cir_all = cir_send + cir_epr12 + cir_mod01  # 量子线路\n",
    "    # get_qs 不会改变系统状态\n",
    "    ket_str = cir_all.get_qs(ket=True, seed=seed)\n",
    "    # 获取q0, q1测量结果，保存在c0, c1\n",
    "    c0, c1 = get_measure_result(ket_str, [0, 1])\n",
    "    # 根据测量结果判断是否加入 X 或 Z 门\n",
    "    if c1 == '1':\n",
    "        cir_all += X(2)\n",
    "    if c0 == '1':\n",
    "        cir_all += Z(2)\n",
    "\n",
    "    send_state = cir_send.get_qs(ket=True, seed=seed)\n",
    "    recv_state = cir_all.get_qs(ket=True, seed=seed)\n",
    "\n",
    "    print(f'Fig.3\\ncomplete circuit:\\n')\n",
    "    display_svg(cir_all.svg())\n",
    "    print(f'Measure result:\\nq0={c0}, q1={c1}\\n')\n",
    "    print(f'Send state ¦qn,...,q0⟩:\\n{send_state}\\n')\n",
    "    print(f'Recv state ¦qn,...,q0⟩:\\n{recv_state}\\n')\n",
    "\n",
    "\n",
    "simulate_fig3()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "169d0f99",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "### 案例2\n",
    "\n",
    "- Fig.5 量子中间方案线路\n",
    "\n",
    "<img src=\"images/fig5.jpg\" width=400>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "3b68acfe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fig.5\n",
      "complete circuit:\n",
      "\n"
     ]
    },
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"576.8\" height=\"320.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"576.8\" height=\"320.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q1: </text><text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q2: </text><text x=\"20.0\" y=\"220.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q3: </text><text x=\"20.0\" y=\"280.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q4: </text><line x1=\"48.8\" x2=\"556.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"556.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"556.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"556.8\" y1=\"220.0\" y2=\"220.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"556.8\" y1=\"280.0\" y2=\"280.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"72.8\" y=\"20.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"112.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RX </text><text x=\"112.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >0.427 </text><rect x=\"172.8\" y=\"20.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"212.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RY </text><text x=\"212.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >0.9587 </text><rect x=\"272.8\" y=\"20.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"312.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RZ </text><text x=\"312.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >1.2577 </text><rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><circle cx=\"152.8\" cy=\"100.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"132.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"138.8\" x2=\"166.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"146.0\" y2=\"174.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><rect x=\"72.8\" y=\"200.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"220.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><circle cx=\"152.8\" cy=\"220.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"220.0\" y2=\"280.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"132.8\" y=\"260.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"138.8\" x2=\"166.8\" y1=\"280.0\" y2=\"280.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"266.0\" y2=\"294.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><circle cx=\"392.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"392.8\" x2=\"392.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"372.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"378.8\" x2=\"406.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"392.8\" x2=\"392.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><rect x=\"432.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"452.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><rect x=\"492.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"512.8\" cy=\"50.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 499.99999999999994 50.4 A 12.8 12.8 0 0 1 525.5999999999999 50.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 516.9018483174859 33.93539030917347 L 525.2156921938165 29.135390309173467 L 525.2156921938165 38.73539030917347 L 521.8901546432843 36.815390309173466 L 514.0470765814496 50.4 L 512.3843078061834 49.44 L 520.2273858680182 35.85539030917347 Z\" fill=\"#ffffff\" /><rect x=\"432.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"452.8\" cy=\"110.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 440.0 110.4 A 12.8 12.8 0 0 1 465.6 110.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 456.9018483174859 93.93539030917347 L 465.21569219381655 89.13539030917347 L 465.21569219381655 98.73539030917347 L 461.8901546432843 96.81539030917347 L 454.04707658144963 110.4 L 452.3843078061835 109.44 L 460.2273858680182 95.85539030917347 Z\" fill=\"#ffffff\" /><rect x=\"192.8\" y=\"140.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"212.8\" y=\"160.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >Z </text><circle cx=\"272.8\" cy=\"160.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"272.8\" x2=\"272.8\" y1=\"160.0\" y2=\"220.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"252.8\" y=\"200.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"258.8\" x2=\"286.8\" y1=\"220.0\" y2=\"220.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"272.8\" x2=\"272.8\" y1=\"206.0\" y2=\"234.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><rect x=\"312.8\" y=\"140.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"332.8\" y=\"160.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><rect x=\"372.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"392.8\" cy=\"170.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 380.0 170.4 A 12.8 12.8 0 0 1 405.6 170.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 396.9018483174859 153.93539030917347 L 405.21569219381655 149.13539030917346 L 405.21569219381655 158.73539030917345 L 401.8901546432843 156.81539030917347 L 394.04707658144963 170.4 L 392.3843078061835 169.44 L 400.2273858680182 155.85539030917346 Z\" fill=\"#ffffff\" /><rect x=\"312.8\" y=\"200.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"332.8\" cy=\"230.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 320.0 230.4 A 12.8 12.8 0 0 1 345.6 230.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 336.9018483174859 213.93539030917347 L 345.21569219381655 209.13539030917346 L 345.21569219381655 218.73539030917345 L 341.8901546432843 216.81539030917347 L 334.04707658144963 230.4 L 332.3843078061835 229.44 L 340.2273858680182 215.85539030917346 Z\" fill=\"#ffffff\" /><rect x=\"192.8\" y=\"260.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"212.8\" y=\"280.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >Z </text></svg>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "measure result:\n",
      "q0 = 1, q1 = 0\n",
      "\n",
      "send state ¦qn,...,q0⟩:\n",
      "(0.7587523655071231-0.4310464601007012j)¦0⟩\n",
      "(0.4750830278097279+0.11309250089701395j)¦1⟩\n",
      "\n",
      "recv state ¦qn,...,q0⟩:\n",
      "(0.7587523655071229-0.43104646010070113j)¦00101⟩\n",
      "(0.4750830278097278+0.11309250089701392j)¦10101⟩\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def simulate_fig5():\n",
    "    \"\"\"复现 Fig.5 线路\"\"\"\n",
    "    cir_send = create_random_state(0)     # the send state |y>\n",
    "    cir_epr1 = create_EPR_state(1, 2)     # EPR Pair [A2, C1]\n",
    "    cir_epr2 = create_EPR_state(3, 4)     # EPR Pair [C2, B]\n",
    "    cir_mod01 = create_basic_module(0, 1)\n",
    "    cir_mod23 = create_basic_module(2, 3)\n",
    "\n",
    "    cir_all = cir_send + cir_epr1 + cir_epr2 + cir_mod01\n",
    "\n",
    "    # 使用同一个随机数种子，避免各次测量结果不同\n",
    "    seed = np.random.randint(0, 0xff)\n",
    "\n",
    "    ## 第一次测量\n",
    "    # get_qs 不会改变系统状态\n",
    "    ket_str = cir_all.get_qs(ket=True, seed=seed)\n",
    "    # 获取q0, q1测量结果，保存在c0, c1\n",
    "    c0, c1 = get_measure_result(ket_str, [0, 1])\n",
    "    # 根据测量结果判断是否加入 X 或 Z 门\n",
    "    if c1 == '1':\n",
    "        cir_all += X(2)\n",
    "    if c0 == '1':\n",
    "        cir_all += Z(2)\n",
    "\n",
    "    ## 第二次测量\n",
    "    cir_all += cir_mod23 # 加上第2部分测量\n",
    "    # get_qs 不会改变系统状态\n",
    "    ket_str = cir_all.get_qs(ket=True, seed=seed)\n",
    "    # 获取q2, q3测量结果，保存在c2, c3\n",
    "    c2, c3 = get_measure_result(ket_str, [2, 3])\n",
    "    # 根据测量结果判断是否加入 X 或 Z 门\n",
    "    if c3 == '1':\n",
    "        cir_all += X(4)\n",
    "    if c2 == '1':\n",
    "        cir_all += Z(4)\n",
    "\n",
    "    print(f'Fig.5\\ncomplete circuit:\\n')\n",
    "    display_svg(cir_all.svg())\n",
    "    print(f'measure result:\\nq0 = {c0}, q1 = {c1}\\n')\n",
    "    print(f'send state ¦qn,...,q0⟩:\\n{cir_send.get_qs(ket=True, seed=seed)}\\n')\n",
    "    print(f'recv state ¦qn,...,q0⟩:\\n{cir_all.get_qs(ket=True, seed=seed)}\\n')\n",
    "\n",
    "\n",
    "simulate_fig5()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "55226037",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "### 案例3\n",
    "\n",
    "- Fig.6 在源节点和宿节点之间通过一个中间节点节点共享EPR对\n",
    "\n",
    "<img src=\"images/fig6.jpg\" width=400>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "9f8e6a73",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fig.6\n",
      "complete circuit:\n",
      "\n"
     ]
    },
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"396.8\" height=\"260.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"396.8\" height=\"260.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q1: </text><text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q2: </text><text x=\"20.0\" y=\"220.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q3: </text><line x1=\"48.8\" x2=\"376.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"376.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"376.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"376.8\" y1=\"220.0\" y2=\"220.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><circle cx=\"152.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"132.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"138.8\" x2=\"166.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><rect x=\"72.8\" y=\"140.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"160.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><circle cx=\"152.8\" cy=\"160.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"160.0\" y2=\"220.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"132.8\" y=\"200.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"138.8\" x2=\"166.8\" y1=\"220.0\" y2=\"220.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"206.0\" y2=\"234.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><circle cx=\"212.8\" cy=\"100.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"212.8\" x2=\"212.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"192.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"198.8\" x2=\"226.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"212.8\" x2=\"212.8\" y1=\"146.0\" y2=\"174.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><rect x=\"252.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"272.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><rect x=\"312.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"332.8\" cy=\"110.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 320.0 110.4 A 12.8 12.8 0 0 1 345.6 110.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 336.9018483174859 93.93539030917347 L 345.21569219381655 89.13539030917347 L 345.21569219381655 98.73539030917347 L 341.8901546432843 96.81539030917347 L 334.04707658144963 110.4 L 332.3843078061835 109.44 L 340.2273858680182 95.85539030917347 Z\" fill=\"#ffffff\" /><rect x=\"252.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"272.8\" cy=\"170.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 260.0 170.4 A 12.8 12.8 0 0 1 285.6 170.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 276.9018483174859 153.93539030917347 L 285.21569219381655 149.13539030917346 L 285.21569219381655 158.73539030917345 L 281.8901546432843 156.81539030917347 L 274.0470765814496 170.4 L 272.3843078061835 169.44 L 280.2273858680182 155.85539030917346 Z\" fill=\"#ffffff\" /><rect x=\"192.8\" y=\"200.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"212.8\" y=\"220.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >Z </text></svg>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "measure result:\n",
      "q1 = 1, q2 = 0\n",
      "\n",
      "recv state ¦qn,...,q0⟩:\n",
      "√2/2¦0010⟩\n",
      "√2/2¦1011⟩\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def simulate_fig6():\n",
    "    cir_epr1 = create_EPR_state(0, 1) # EPR Pair [A, C1]\n",
    "    cir_epr2 = create_EPR_state(2, 3) # EPR Pair [C2, B]\n",
    "    cir_mod12 = create_basic_module(1, 2)\n",
    "    cir_all = cir_epr1 + cir_epr2 + cir_mod12\n",
    "\n",
    "    # 使用同一个随机数种子，避免各次测量结果不同\n",
    "    seed = np.random.randint(0, 0xff) # 随机数种子\n",
    "\n",
    "    # get_qs 不会改变系统状态\n",
    "    ket_str = cir_all.get_qs(ket=True, seed=seed)\n",
    "    # 获取q0, q1测量结果，保存在c0, c1\n",
    "    c1, c2 = get_measure_result(ket_str, [1, 2])\n",
    "    # 根据测量结果判断是否加入 X 或 Z 门\n",
    "    if c2 == '1':\n",
    "        cir_all += X(3)\n",
    "    if c1 == '1':\n",
    "        cir_all += Z(3)\n",
    "\n",
    "    print(f'Fig.6\\ncomplete circuit:\\n')\n",
    "    display_svg(cir_all.svg())\n",
    "    print(f'measure result:\\nq1 = {c1}, q2 = {c2}\\n')\n",
    "    print(f'recv state ¦qn,...,q0⟩:\\n{cir_all.get_qs(ket=True, seed=seed)}\\n')\n",
    "\n",
    "simulate_fig6()\n",
    "# |0xx0> + |1xx1>"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "670bbcec",
   "metadata": {},
   "source": [
    "### 案例4\n",
    "\n",
    "- Fig.8 使用EPR桥接传输单个量子态\n",
    "\n",
    "<img src=\"images/fig8.jpg\" width=400>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "9d64838d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fig.8\n",
      "complete circuit:\n",
      "\n"
     ]
    },
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"576.8\" height=\"320.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"576.8\" height=\"320.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q1: </text><text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q2: </text><text x=\"20.0\" y=\"220.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q3: </text><text x=\"20.0\" y=\"280.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q4: </text><line x1=\"48.8\" x2=\"556.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"556.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"556.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"556.8\" y1=\"220.0\" y2=\"220.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"556.8\" y1=\"280.0\" y2=\"280.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"72.8\" y=\"20.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"112.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RX </text><text x=\"112.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >0.8094 </text><rect x=\"172.8\" y=\"20.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"212.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RY </text><text x=\"212.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >4.0381 </text><rect x=\"272.8\" y=\"20.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"312.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RZ </text><text x=\"312.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >4.9893 </text><rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><circle cx=\"152.8\" cy=\"100.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"132.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"138.8\" x2=\"166.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"146.0\" y2=\"174.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><rect x=\"72.8\" y=\"200.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"220.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><circle cx=\"152.8\" cy=\"220.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"220.0\" y2=\"280.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"132.8\" y=\"260.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"138.8\" x2=\"166.8\" y1=\"280.0\" y2=\"280.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"266.0\" y2=\"294.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><circle cx=\"212.8\" cy=\"160.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"212.8\" x2=\"212.8\" y1=\"160.0\" y2=\"220.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"192.8\" y=\"200.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"198.8\" x2=\"226.8\" y1=\"220.0\" y2=\"220.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"212.8\" x2=\"212.8\" y1=\"206.0\" y2=\"234.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><rect x=\"252.8\" y=\"140.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"272.8\" y=\"160.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><rect x=\"312.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"332.8\" cy=\"170.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 320.0 170.4 A 12.8 12.8 0 0 1 345.6 170.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 336.9018483174859 153.93539030917347 L 345.21569219381655 149.13539030917346 L 345.21569219381655 158.73539030917345 L 341.8901546432843 156.81539030917347 L 334.04707658144963 170.4 L 332.3843078061835 169.44 L 340.2273858680182 155.85539030917346 Z\" fill=\"#ffffff\" /><rect x=\"252.8\" y=\"200.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"272.8\" cy=\"230.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 260.0 230.4 A 12.8 12.8 0 0 1 285.6 230.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 276.9018483174859 213.93539030917347 L 285.21569219381655 209.13539030917346 L 285.21569219381655 218.73539030917345 L 281.8901546432843 216.81539030917347 L 274.0470765814496 230.4 L 272.3843078061835 229.44 L 280.2273858680182 215.85539030917346 Z\" fill=\"#ffffff\" /><circle cx=\"392.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"392.8\" x2=\"392.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"372.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"378.8\" x2=\"406.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"392.8\" x2=\"392.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><rect x=\"432.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"452.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><rect x=\"492.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"512.8\" cy=\"50.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 499.99999999999994 50.4 A 12.8 12.8 0 0 1 525.5999999999999 50.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 516.9018483174859 33.93539030917347 L 525.2156921938165 29.135390309173467 L 525.2156921938165 38.73539030917347 L 521.8901546432843 36.815390309173466 L 514.0470765814496 50.4 L 512.3843078061834 49.44 L 520.2273858680182 35.85539030917347 Z\" fill=\"#ffffff\" /><rect x=\"432.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"452.8\" cy=\"110.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 440.0 110.4 A 12.8 12.8 0 0 1 465.6 110.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 456.9018483174859 93.93539030917347 L 465.21569219381655 89.13539030917347 L 465.21569219381655 98.73539030917347 L 461.8901546432843 96.81539030917347 L 454.04707658144963 110.4 L 452.3843078061835 109.44 L 460.2273858680182 95.85539030917347 Z\" fill=\"#ffffff\" /><rect x=\"192.8\" y=\"260.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"212.8\" y=\"280.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >Z </text></svg>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "measure result:\n",
      "q3=0, q2=0, q1=0, q0=1\n",
      "\n",
      "send state ¦qn,...,q0⟩:\n",
      "(0.5317769838094857-0.04298993988258498j)¦0⟩\n",
      "(-0.7638590799214289+0.3631589329494264j)¦1⟩\n",
      "\n",
      "recv state ¦qn,...,q0⟩:\n",
      "(0.5317769838094857-0.042989939882584996j)¦00001⟩\n",
      "(-0.763859079921429+0.36315893294942647j)¦10001⟩\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def simulate_fig8():\n",
    "    cir_send = create_random_state(0)\n",
    "    cir_epr12 = create_EPR_state(1, 2)\n",
    "    cir_epr34 = create_EPR_state(3, 4)\n",
    "    cir_mod23 = create_basic_module(2, 3)\n",
    "    cir_mod01 = create_basic_module(0, 1)\n",
    "\n",
    "    cir_all = cir_send + cir_epr12 + cir_epr34 + cir_mod23\n",
    "\n",
    "    # 使用同一个随机数种子，避免各次测量结果不同\n",
    "    seed = np.random.randint(0, 0xff)\n",
    "\n",
    "    ## 第一次测量\n",
    "    # get_qs 不会改变系统状态\n",
    "    ket_str = cir_all.get_qs(ket=True, seed=seed)\n",
    "    # 获取q2, q3测量结果，保存在c2, c3\n",
    "    c2, c3 = get_measure_result(ket_str, [2, 3])\n",
    "    # 根据测量结果判断是否加入 X 或 Z 门\n",
    "    if c3 == '1':\n",
    "        cir_all += X(4)\n",
    "    if c2 == '1':\n",
    "        cir_all += Z(4)\n",
    "\n",
    "    ## 第二次测量\n",
    "    cir_all += cir_mod01 # 加上第2部分测量\n",
    "    # get_qs 不会改变系统状态\n",
    "    ket_str = cir_all.get_qs(ket=True, seed=seed)\n",
    "    # 获取q0, q2测量结果，保存在c0, c1\n",
    "    c0, c1 = get_measure_result(ket_str, [0, 1])\n",
    "    # 根据测量结果判断是否加入 X 或 Z 门\n",
    "    if c1 == '1':\n",
    "        cir_all += X(4)\n",
    "    if c0 == '1':\n",
    "        cir_all += Z(4)\n",
    "\n",
    "    print(f'Fig.8\\ncomplete circuit:\\n')\n",
    "    display_svg(cir_all.svg())\n",
    "    print(f'measure result:\\nq3={c3}, q2={c2}, q1={c1}, q0={c0}\\n')\n",
    "    print(f'send state ¦qn,...,q0⟩:\\n{cir_send.get_qs(ket=True, seed=seed)}\\n')\n",
    "    print(f'recv state ¦qn,...,q0⟩:\\n{cir_all.get_qs(ket=True, seed=seed)}\\n')\n",
    "\n",
    "\n",
    "simulate_fig8()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "cf96e9e9",
   "metadata": {},
   "source": [
    "### 案例5\n",
    "\n",
    "- Fig.9 实现量子路由机制的量子线路\n",
    "\n",
    "<img src=\"images/fig9.jpg\" width=400>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "1263680c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fig.9\n",
      "complete circuit:\n",
      "\n"
     ]
    },
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"576.8\" height=\"320.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"576.8\" height=\"320.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q1: </text><text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q2: </text><text x=\"20.0\" y=\"220.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q3: </text><text x=\"20.0\" y=\"280.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q4: </text><line x1=\"48.8\" x2=\"556.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"556.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"556.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"556.8\" y1=\"220.0\" y2=\"220.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"556.8\" y1=\"280.0\" y2=\"280.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"72.8\" y=\"20.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"112.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RX </text><text x=\"112.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >2.4098 </text><rect x=\"172.8\" y=\"20.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"212.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RY </text><text x=\"212.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >1.0651 </text><rect x=\"272.8\" y=\"20.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"312.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RZ </text><text x=\"312.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >2.2439 </text><rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><circle cx=\"152.8\" cy=\"100.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"132.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"138.8\" x2=\"166.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"146.0\" y2=\"174.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><rect x=\"72.8\" y=\"200.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"220.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><circle cx=\"152.8\" cy=\"220.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"220.0\" y2=\"280.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"132.8\" y=\"260.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"138.8\" x2=\"166.8\" y1=\"280.0\" y2=\"280.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"266.0\" y2=\"294.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><circle cx=\"392.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"392.8\" x2=\"392.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"372.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"378.8\" x2=\"406.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"392.8\" x2=\"392.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><rect x=\"432.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"452.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><rect x=\"492.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"512.8\" cy=\"50.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 499.99999999999994 50.4 A 12.8 12.8 0 0 1 525.5999999999999 50.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 516.9018483174859 33.93539030917347 L 525.2156921938165 29.135390309173467 L 525.2156921938165 38.73539030917347 L 521.8901546432843 36.815390309173466 L 514.0470765814496 50.4 L 512.3843078061834 49.44 L 520.2273858680182 35.85539030917347 Z\" fill=\"#ffffff\" /><rect x=\"432.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"452.8\" cy=\"110.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 440.0 110.4 A 12.8 12.8 0 0 1 465.6 110.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 456.9018483174859 93.93539030917347 L 465.21569219381655 89.13539030917347 L 465.21569219381655 98.73539030917347 L 461.8901546432843 96.81539030917347 L 454.04707658144963 110.4 L 452.3843078061835 109.44 L 460.2273858680182 95.85539030917347 Z\" fill=\"#ffffff\" /><circle cx=\"212.8\" cy=\"160.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"212.8\" x2=\"212.8\" y1=\"160.0\" y2=\"220.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"192.8\" y=\"200.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"198.8\" x2=\"226.8\" y1=\"220.0\" y2=\"220.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"212.8\" x2=\"212.8\" y1=\"206.0\" y2=\"234.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><rect x=\"252.8\" y=\"140.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"272.8\" y=\"160.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><rect x=\"312.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"332.8\" cy=\"170.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 320.0 170.4 A 12.8 12.8 0 0 1 345.6 170.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 336.9018483174859 153.93539030917347 L 345.21569219381655 149.13539030917346 L 345.21569219381655 158.73539030917345 L 341.8901546432843 156.81539030917347 L 334.04707658144963 170.4 L 332.3843078061835 169.44 L 340.2273858680182 155.85539030917346 Z\" fill=\"#ffffff\" /><rect x=\"252.8\" y=\"200.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"272.8\" cy=\"230.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 260.0 230.4 A 12.8 12.8 0 0 1 285.6 230.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 276.9018483174859 213.93539030917347 L 285.21569219381655 209.13539030917346 L 285.21569219381655 218.73539030917345 L 281.8901546432843 216.81539030917347 L 274.0470765814496 230.4 L 272.3843078061835 229.44 L 280.2273858680182 215.85539030917346 Z\" fill=\"#ffffff\" /><rect x=\"192.8\" y=\"260.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"212.8\" y=\"280.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >X </text></svg>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Measure result:\n",
      "q3=0, q2=0, q1=1, q0=0\n",
      "\n",
      "Send state ¦qn,...,q0⟩:\n",
      "(0.5609078399795123-0.07199547988470448j)¦0⟩\n",
      "(0.8036306917025748-0.18540970114675784j)¦1⟩\n",
      "\n",
      "Recv state ¦qn,...,q0⟩:\n",
      "(0.5609078399795128-0.07199547988470453j)¦00010⟩\n",
      "(0.8036306917025755-0.18540970114675795j)¦10010⟩\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def simulate_fig9():\n",
    "    cir_send = create_random_state(0)\n",
    "    cir_epr12 = create_EPR_state(1, 2)\n",
    "    cir_epr34 = create_EPR_state(3, 4)\n",
    "    cir_mod01 = create_basic_module(0, 1)\n",
    "    cir_mod23 = create_basic_module(2, 3)\n",
    "\n",
    "    cir_all = cir_send + cir_epr12 + cir_epr34 + cir_mod01 + cir_mod23\n",
    "\n",
    "    # 使用同一个随机数种子，避免各次测量结果不同\n",
    "    seed = np.random.randint(0, 0xff) # 随机数种子\n",
    "\n",
    "    # get_qs 不会改变系统状态\n",
    "    ket_str = cir_all.get_qs(ket=True, seed=seed)\n",
    "    # 获取q0-q3测量结果，保存在c0-c3\n",
    "    c0, c1, c2, c3 = get_measure_result(ket_str, [0, 1, 2, 3])\n",
    "    a1, a2, cc1, cc2 = c0, c1, c2, c3\n",
    "    # 根据测量结果判断是否加入 X 或 Z 门\n",
    "    if a2 != cc2: # XOR\n",
    "        cir_all += X(4)\n",
    "    if a1 != cc1: # XOR\n",
    "        cir_all += Z(4)\n",
    "\n",
    "    print(f'Fig.9\\ncomplete circuit:\\n')\n",
    "    display_svg(cir_all.svg())\n",
    "    print(f'Measure result:\\nq3={c3}, q2={c2}, q1={c1}, q0={c0}\\n')\n",
    "    print(f'Send state ¦qn,...,q0⟩:\\n{cir_send.get_qs(ket=True, seed=seed)}\\n')\n",
    "    print(f'Recv state ¦qn,...,q0⟩:\\n{cir_all.get_qs(ket=True, seed=seed)}\\n')\n",
    "\n",
    "\n",
    "simulate_fig9()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "d2fdce3a",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "### 案例6\n",
    "\n",
    "- Fig.13 Alice和Bob在同一个QBK范围内的量子线路\n",
    "\n",
    "<img src=\"images/fig13.jpg\" width=400>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a6bd4a9b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fig.13\n",
      "complete circuit:\n",
      "\n"
     ]
    },
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"576.8\" height=\"320.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"576.8\" height=\"320.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q1: </text><text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q2: </text><text x=\"20.0\" y=\"220.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q3: </text><text x=\"20.0\" y=\"280.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q4: </text><line x1=\"48.8\" x2=\"556.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"556.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"556.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"556.8\" y1=\"220.0\" y2=\"220.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"556.8\" y1=\"280.0\" y2=\"280.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"72.8\" y=\"20.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"112.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RX </text><text x=\"112.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >1.9492 </text><rect x=\"172.8\" y=\"20.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"212.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RY </text><text x=\"212.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >3.5143 </text><rect x=\"272.8\" y=\"20.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"312.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RZ </text><text x=\"312.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >4.5308 </text><rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><circle cx=\"152.8\" cy=\"100.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"132.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"138.8\" x2=\"166.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"146.0\" y2=\"174.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><rect x=\"72.8\" y=\"200.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"220.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><circle cx=\"152.8\" cy=\"220.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"220.0\" y2=\"280.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"132.8\" y=\"260.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"138.8\" x2=\"166.8\" y1=\"280.0\" y2=\"280.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"266.0\" y2=\"294.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><circle cx=\"392.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"392.8\" x2=\"392.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"372.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"378.8\" x2=\"406.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"392.8\" x2=\"392.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><rect x=\"432.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"452.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><rect x=\"492.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"512.8\" cy=\"50.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 499.99999999999994 50.4 A 12.8 12.8 0 0 1 525.5999999999999 50.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 516.9018483174859 33.93539030917347 L 525.2156921938165 29.135390309173467 L 525.2156921938165 38.73539030917347 L 521.8901546432843 36.815390309173466 L 514.0470765814496 50.4 L 512.3843078061834 49.44 L 520.2273858680182 35.85539030917347 Z\" fill=\"#ffffff\" /><rect x=\"432.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"452.8\" cy=\"110.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 440.0 110.4 A 12.8 12.8 0 0 1 465.6 110.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 456.9018483174859 93.93539030917347 L 465.21569219381655 89.13539030917347 L 465.21569219381655 98.73539030917347 L 461.8901546432843 96.81539030917347 L 454.04707658144963 110.4 L 452.3843078061835 109.44 L 460.2273858680182 95.85539030917347 Z\" fill=\"#ffffff\" /><circle cx=\"212.8\" cy=\"160.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"212.8\" x2=\"212.8\" y1=\"160.0\" y2=\"220.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"192.8\" y=\"200.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"198.8\" x2=\"226.8\" y1=\"220.0\" y2=\"220.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"212.8\" x2=\"212.8\" y1=\"206.0\" y2=\"234.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><rect x=\"252.8\" y=\"140.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"272.8\" y=\"160.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><rect x=\"312.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"332.8\" cy=\"170.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 320.0 170.4 A 12.8 12.8 0 0 1 345.6 170.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 336.9018483174859 153.93539030917347 L 345.21569219381655 149.13539030917346 L 345.21569219381655 158.73539030917345 L 341.8901546432843 156.81539030917347 L 334.04707658144963 170.4 L 332.3843078061835 169.44 L 340.2273858680182 155.85539030917346 Z\" fill=\"#ffffff\" /><rect x=\"252.8\" y=\"200.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"272.8\" cy=\"230.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 260.0 230.4 A 12.8 12.8 0 0 1 285.6 230.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 276.9018483174859 213.93539030917347 L 285.21569219381655 209.13539030917346 L 285.21569219381655 218.73539030917345 L 281.8901546432843 216.81539030917347 L 274.0470765814496 230.4 L 272.3843078061835 229.44 L 280.2273858680182 215.85539030917346 Z\" fill=\"#ffffff\" /><rect x=\"192.8\" y=\"260.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"212.8\" y=\"280.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >X </text></svg>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Measure result:\n",
      "q3=0, q2=0, q1=1, q0=0\n",
      "\n",
      "Send state ¦qn,...,q0⟩:\n",
      "(0.6913383362651505-0.44053791923099206j)¦0⟩\n",
      "(-0.4709942258759291+0.3257945452601929j)¦1⟩\n",
      "\n",
      "Recv state ¦qn,...,q0⟩:\n",
      "(0.6913383362651504-0.4405379192309919j)¦00010⟩\n",
      "(-0.47099422587592904+0.32579454526019286j)¦10010⟩\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def simulate_fig13():\n",
    "    \"\"\"same to fig9\"\"\"\n",
    "    cir_send = create_random_state(0)\n",
    "    cir_epr12 = create_EPR_state(1, 2)\n",
    "    cir_epr34 = create_EPR_state(3, 4)\n",
    "    cir_mod01 = create_basic_module(0, 1)\n",
    "    cir_mod23 = create_basic_module(2, 3)\n",
    "\n",
    "    cir_all = cir_send + cir_epr12 + cir_epr34 + cir_mod01 + cir_mod23\n",
    "\n",
    "    # 使用同一个随机数种子，避免各次测量结果不同\n",
    "    seed = np.random.randint(0, 0xff)\n",
    "\n",
    "    # get_qs 不会改变系统状态\n",
    "    ket_str = cir_all.get_qs(ket=True, seed=seed)\n",
    "    # 获取q0-q3测量结果，保存在c0-c3\n",
    "    c0, c1, c2, c3 = get_measure_result(ket_str, [0, 1, 2, 3])\n",
    "    a1, a2, cc1, cc2 = c0, c1, c2, c3\n",
    "    # 根据测量结果判断是否加入 X 或 Z 门\n",
    "    if a2 != cc2:  # XOR\n",
    "        cir_all += X(4)\n",
    "    if a1 != cc1:  # XOR\n",
    "        cir_all += Z(4)\n",
    "\n",
    "    print(f'Fig.13\\ncomplete circuit:\\n')\n",
    "    display_svg(cir_all.svg())\n",
    "    print(f'Measure result:\\nq3={c3}, q2={c2}, q1={c1}, q0={c0}\\n')\n",
    "    print(f'Send state ¦qn,...,q0⟩:\\n{cir_send.get_qs(ket=True, seed=seed)}\\n')\n",
    "    print(f'Recv state ¦qn,...,q0⟩:\\n{cir_all.get_qs(ket=True, seed=seed)}\\n')\n",
    "\n",
    "\n",
    "simulate_fig13()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "cd2de444",
   "metadata": {},
   "source": [
    "### 案例7\n",
    "\n",
    "- Fig.14 Alice和Bob在不同QBK域内时的量子线路\n",
    "\n",
    "<img src=\"images/fig14.jpg\" width=400>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "18e10f8b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fig.14\n",
      "complete circuit:\n",
      "\n"
     ]
    },
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"576.8\" height=\"440.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"576.8\" height=\"440.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q1: </text><text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q2: </text><text x=\"20.0\" y=\"220.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q3: </text><text x=\"20.0\" y=\"280.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q4: </text><text x=\"20.0\" y=\"340.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q5: </text><text x=\"20.0\" y=\"400.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q6: </text><line x1=\"48.8\" x2=\"556.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"556.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"556.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"556.8\" y1=\"220.0\" y2=\"220.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"556.8\" y1=\"280.0\" y2=\"280.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"556.8\" y1=\"340.0\" y2=\"340.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"556.8\" y1=\"400.0\" y2=\"400.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"72.8\" y=\"20.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"112.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RX </text><text x=\"112.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >1.645 </text><rect x=\"172.8\" y=\"20.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"212.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RY </text><text x=\"212.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >6.0898 </text><rect x=\"272.8\" y=\"20.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"312.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RZ </text><text x=\"312.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >2.6084 </text><rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><circle cx=\"152.8\" cy=\"100.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"132.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"138.8\" x2=\"166.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"146.0\" y2=\"174.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><rect x=\"72.8\" y=\"200.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"220.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><circle cx=\"152.8\" cy=\"220.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"220.0\" y2=\"280.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"132.8\" y=\"260.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"138.8\" x2=\"166.8\" y1=\"280.0\" y2=\"280.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"266.0\" y2=\"294.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><rect x=\"72.8\" y=\"320.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"340.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><circle cx=\"152.8\" cy=\"340.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"340.0\" y2=\"400.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"132.8\" y=\"380.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"138.8\" x2=\"166.8\" y1=\"400.0\" y2=\"400.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"386.0\" y2=\"414.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><circle cx=\"392.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"392.8\" x2=\"392.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"372.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"378.8\" x2=\"406.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"392.8\" x2=\"392.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><rect x=\"432.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"452.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><rect x=\"492.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"512.8\" cy=\"50.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 499.99999999999994 50.4 A 12.8 12.8 0 0 1 525.5999999999999 50.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 516.9018483174859 33.93539030917347 L 525.2156921938165 29.135390309173467 L 525.2156921938165 38.73539030917347 L 521.8901546432843 36.815390309173466 L 514.0470765814496 50.4 L 512.3843078061834 49.44 L 520.2273858680182 35.85539030917347 Z\" fill=\"#ffffff\" /><rect x=\"432.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"452.8\" cy=\"110.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 440.0 110.4 A 12.8 12.8 0 0 1 465.6 110.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 456.9018483174859 93.93539030917347 L 465.21569219381655 89.13539030917347 L 465.21569219381655 98.73539030917347 L 461.8901546432843 96.81539030917347 L 454.04707658144963 110.4 L 452.3843078061835 109.44 L 460.2273858680182 95.85539030917347 Z\" fill=\"#ffffff\" /><circle cx=\"212.8\" cy=\"160.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"212.8\" x2=\"212.8\" y1=\"160.0\" y2=\"220.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"192.8\" y=\"200.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"198.8\" x2=\"226.8\" y1=\"220.0\" y2=\"220.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"212.8\" x2=\"212.8\" y1=\"206.0\" y2=\"234.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><rect x=\"252.8\" y=\"140.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"272.8\" y=\"160.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><rect x=\"312.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"332.8\" cy=\"170.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 320.0 170.4 A 12.8 12.8 0 0 1 345.6 170.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 336.9018483174859 153.93539030917347 L 345.21569219381655 149.13539030917346 L 345.21569219381655 158.73539030917345 L 341.8901546432843 156.81539030917347 L 334.04707658144963 170.4 L 332.3843078061835 169.44 L 340.2273858680182 155.85539030917346 Z\" fill=\"#ffffff\" /><rect x=\"252.8\" y=\"200.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"272.8\" cy=\"230.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 260.0 230.4 A 12.8 12.8 0 0 1 285.6 230.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 276.9018483174859 213.93539030917347 L 285.21569219381655 209.13539030917346 L 285.21569219381655 218.73539030917345 L 281.8901546432843 216.81539030917347 L 274.0470765814496 230.4 L 272.3843078061835 229.44 L 280.2273858680182 215.85539030917346 Z\" fill=\"#ffffff\" /><circle cx=\"212.8\" cy=\"280.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"212.8\" x2=\"212.8\" y1=\"280.0\" y2=\"340.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"192.8\" y=\"320.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"198.8\" x2=\"226.8\" y1=\"340.0\" y2=\"340.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"212.8\" x2=\"212.8\" y1=\"326.0\" y2=\"354.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><rect x=\"252.8\" y=\"260.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"272.8\" y=\"280.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><rect x=\"312.8\" y=\"260.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"332.8\" cy=\"290.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 320.0 290.4 A 12.8 12.8 0 0 1 345.6 290.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 336.9018483174859 273.9353903091735 L 345.21569219381655 269.13539030917343 L 345.21569219381655 278.73539030917345 L 341.8901546432843 276.8153903091735 L 334.04707658144963 290.4 L 332.3843078061835 289.44 L 340.2273858680182 275.85539030917346 Z\" fill=\"#ffffff\" /><rect x=\"252.8\" y=\"320.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"272.8\" cy=\"350.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 260.0 350.4 A 12.8 12.8 0 0 1 285.6 350.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 276.9018483174859 333.93539030917344 L 285.21569219381655 329.1353903091735 L 285.21569219381655 338.73539030917345 L 281.8901546432843 336.8153903091735 L 274.0470765814496 350.4 L 272.3843078061835 349.44 L 280.2273858680182 335.85539030917346 Z\" fill=\"#ffffff\" /></svg>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Measure result:\n",
      "q5=1, q4=1, q3=0, q2=0, q1=1, q0=1\n",
      "\n",
      "Send state ¦qn,...,q0⟩:\n",
      "(-0.11013842684602972+0.6719143380846326j)¦0⟩\n",
      "(-0.6863639707062489+0.2555487212343731j)¦1⟩\n",
      "\n",
      "Recv state ¦qn,...,q0⟩:\n",
      "(0.11013842684602969-0.6719143380846326j)¦0110011⟩\n",
      "(0.6863639707062489-0.255548721234373j)¦1110011⟩\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def simulate_fig14():\n",
    "    \"\"\"复现 Fig.14 线路\"\"\"\n",
    "    cir_send = create_random_state(0)  # the send state\n",
    "    cir_epr12 = create_EPR_state(1, 2)\n",
    "    cir_epr34 = create_EPR_state(3, 4)\n",
    "    cir_epr56 = create_EPR_state(5, 6)\n",
    "\n",
    "    cir_mod01 = create_basic_module(0, 1)\n",
    "    cir_mod23 = create_basic_module(2, 3)\n",
    "    cir_mod45 = create_basic_module(4, 5)\n",
    "\n",
    "    cir_all = cir_send + cir_epr12 + cir_epr34 + cir_epr56 +\\\n",
    "                cir_mod01 + cir_mod23 + cir_mod45\n",
    "\n",
    "    # 使用同一个随机数种子，避免各次测量结果不同\n",
    "    seed = np.random.randint(0, 0xff)\n",
    "\n",
    "    # get_qs 不会改变系统状态\n",
    "    ket_str = cir_all.get_qs(ket=True, seed=seed)\n",
    "    # 获取q0-q3测量结果，保存在c0-c3\n",
    "    c0, c1, c2, c3, c4, c5 = get_measure_result(ket_str, [0, 1, 2, 3, 4, 5])\n",
    "    # 根据测量结果判断是否加入 X 或 Z 门\n",
    "    i0, i1, i2, i3, i4, i5 = int(c0), int(c1), int(c2), \\\n",
    "                                int(c3), int(c4), int(c5)\n",
    "    if i1+i3+i5 in [1, 3]:  # XOR\n",
    "        cir_all += X(6)\n",
    "    if i0+i2+i4 in [1, 3]:  # XOR\n",
    "        cir_all += Z(6)\n",
    "\n",
    "    print(f'Fig.14\\ncomplete circuit:\\n')\n",
    "    display_svg(cir_all.svg())\n",
    "    print(f'Measure result:\\nq5={c5}, q4={c4}, q3={c3}, q2={c2}, q1={c1}, q0={c0}\\n')\n",
    "    print(f'Send state ¦qn,...,q0⟩:\\n{cir_send.get_qs(ket=True, seed=seed)}\\n')\n",
    "    print(f'Recv state ¦qn,...,q0⟩:\\n{cir_all.get_qs(ket=True, seed=seed)}\\n')\n",
    "\n",
    "\n",
    "simulate_fig14()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "vscode": {
   "interpreter": {
    "hash": "9e29c145b53c9f4751736325a7cae7425ef0693c1192ca6a84dea59eaad784ff"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
